:varThe :var header argument is used to pass
arguments to code blocks. The specifics of how arguments are
included in a code block vary by language; these are addressed in
the language-specific documentation. However, the syntax used to
specify arguments is the same across all languages. The values
passed to arguments can be literal values, values from org-mode
tables and literal example blocks, or the results of other code
blocks.
These values can be indexed in a manner similar to arrays—see the “indexable variable values” heading below.
The following syntax is used to pass arguments to code blocks
using the :var header argument.
:var name=assign
where assign can take one of the following
forms
"string" or a
number 9.
#+tblname: example-table
| 1 |
| 2 |
| 3 |
| 4 |
#+source: table-length
#+begin_src emacs-lisp :var table=example-table
(length table)
#+end_src
#+results: table-length
: 4
a code block name, as assigned by #+srcname:,
followed by parentheses:
#+begin_src emacs-lisp :var length=table-length()
(* 2 length)
#+end_src
#+results:
: 8
In addition, an argument can be passed to the code block
referenced by :var. The argument is passed
within the parentheses following the code block name:
#+source: double
#+begin_src emacs-lisp :var input=8
(* 2 input)
#+end_src
#+results: double
: 16
#+source: squared
#+begin_src emacs-lisp :var input=double(input=1)
(* input input)
#+end_src
#+results: squared
: 4
It is also possible to specify arguments in a potentially more
natural way using the #+source: line of a code
block. As in the following example arguments can be packed inside
of parenthesis, separated by commas, following the source
name.
#+source: double(input=0, x=2)
#+begin_src emacs-lisp
(* 2 (+ input x))
#+end_src
It is possible to reference portions of variable values by
“indexing” into the variables. Indexes are 0 based
with negative values counting back from the end. If an index is
separated by ,s then each subsequent section will
index into the next deepest nesting or dimension of the value.
The following example assigns the last cell of the first row the
table example-table to the variable
data:
#+results: example-table
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
#+begin_src emacs-lisp :var data=example-table[0,-1]
data
#+end_src
#+results:
: a
Ranges of variable values can be referenced using two integers
separated by a :, in which case the entire inclusive
range is referenced. For example the following assigns the middle
three rows of example-table to
data.
#+results: example-table
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | 3 |
#+begin_src emacs-lisp :var data=example-table[1:3]
data
#+end_src
#+results:
| 2 | b |
| 3 | c |
| 4 | d |
Additionally, an empty index, or the single character
*, are both interpreted to mean the entire range and
as such are equivalent to 0:-1, as shown in the
following example in which the entire first column is
referenced.
#+results: example-table
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
#+begin_src emacs-lisp :var data=example-table[,0]
data
#+end_src
#+results:
| 1 | 2 | 3 | 4 |
It is possible to index into the results of code blocks as well as tables. Any number of dimensions can be indexed. Dimensions are separated from one another by commas, as shown in the following example.
#+source: 3D
#+begin_src emacs-lisp
'(((1 2 3) (4 5 6) (7 8 9))
((10 11 12) (13 14 15) (16 17 18))
((19 20 21) (22 23 24) (25 26 27)))
#+end_src
#+begin_src emacs-lisp :var data=3D[1,,1]
data
#+end_src
#+results:
| 11 | 14 | 17 |